package com.atguigu.crowd.service.impl;

import com.atguigu.crowd.mapper.AuthMapper;
import com.atguigu.crowd.service.api.AuthService;
import crowd.entity.Auth;
import crowd.entity.AuthExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service
public class AuthServiceImpl implements AuthService {

    @Autowired
    private AuthMapper authMapper;

    @Override
    public List<Auth> getAllAuth() {

        return authMapper.selectByExample(new AuthExample());

    }

    @Override
    public List<Integer> getAssginedAuthIdByRoleId(Integer roleId) {

        List<Integer> roleIdList = authMapper.selectAssignedAuthIdByRoleId(roleId);

        return roleIdList;
    }

    @Override
    public void saveRoleAuthRelationship(Map<String, List<Integer>> map) {

        // 将role取出
        List<Integer> roleIdList = map.get("roleId");
        Integer roleId = roleIdList.get(0);

        // 删除roleId的旧数据
        authMapper.deleteOldRelationship(roleId);

        // 取出新添加的权限信息
        List<Integer> authIdArray = map.get("authIdArray");

        // 将新的权限添加至数据库
        if (authIdArray != null && authIdArray.size() != 0){

            authMapper.insertNewRelationship(roleId,authIdArray);
        }

    }

    @Override
    public List<String> getAssginedAuthNameByAdminId(Integer adminId) {
        return authMapper.selectAssginedAuthNameByAdminId(adminId);
    }
}
